# This is a sample Python script.
import json
from datetime import datetime

import requests
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 获取链接池、ORM表对象
from sqlalchemy import (
  create_engine,
  Column,
  Integer,
  String,
  Enum,
  DECIMAL,
  DateTime,
  Boolean,
  UniqueConstraint,
  Index
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session

# 基础类
Base = declarative_base()

# 创建引擎
engine = create_engine(
    "mysql+pymysql://root:endless@localhost:3306/shein?charset=utf8mb4",
    # "mysql+pymysql://tom@127.0.0.1:3306/db1?charset=utf8mb4", # 无密码时
    # 超过链接池大小外最多创建的链接
    max_overflow=0,
    # 链接池大小
    pool_size=5,
    # 链接池中没有可用链接则最多等待的秒数，超过该秒数后报错
    pool_timeout=10,
    # 多久之后对链接池中的链接进行一次回收
    pool_recycle=1,
    # 查看原生语句（未格式化）
    echo=True
)
# 绑定引擎
Session = sessionmaker(bind=engine)
# 创建数据库链接池，直接使用session即可为当前线程拿出一个链接对象conn
# 内部会采用threading.local进行隔离
session = scoped_session(Session)

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium import webdriver
from selenium.webdriver.common.by import By

# Chrome浏览器
driver = webdriver.Chrome()


class Product(Base):
  # 数据库中存储的表名
  __tablename__ = "product_list"
  id = Column(Integer, primary_key=True, autoincrement=True, comment="主键")
  product_url = Column(String(1000), index=False, nullable=True,
                       comment="产品地址")
  product_name = Column(String(1000), index=True, nullable=True,
                        comment="产品名称")
  product_price = Column(String(32), index=False, nullable=True,
                         comment="产品价格")
  product_image = Column(String(1000), index=False, nullable=True,
                         comment="产品首页")
  nacional_name = Column(String(32), index=False, nullable=True,
                         comment="是否全球")
  sku = Column(String(32), index=False, nullable=True,
               comment="sku")
  spu = Column(String(32), index=False, nullable=True,
               comment="spu")
  start_text = Column(String(32), index=False, nullable=True,
                      comment="近期评论数")
  near_sale_count = Column(String(32), index=False, nullable=True,
                           comment="90天购买人数")
  create_time = Column(
      DateTime, default=datetime.now, comment="创建时间")
  last_update_time = Column(
      DateTime, onupdate=datetime.now, comment="最后更新时间")

  def __init__(self, productUrl, productName, productPrice, productImage,
      nacionalName, startText, nearSaleCount, sku, spu):
    self.product_url = productUrl
    self.product_name = productName
    self.product_price = productPrice
    self.product_image = productImage
    self.nacional_name = nacionalName
    self.start_text = startText
    self.near_sale_count = nearSaleCount
    self.sku = sku
    self.spu = spu


def print_hi(name):
  # Use a breakpoint in the code line below to debug your script.
  print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
  # 创建表 
  # Base.metadata.create_all(engine)

  driver.get(
      "https://us.shein.com/pdsearch/summer%20sleeping%20mat/?ici=s1`EditSearch`summer%20sleeping%20mat`_fb`d0`PageRealClass&search_source=1&search_type=all&src_identifier=st%3D2%60sc%3Dsummer%20sleeping%20mat%60sr%3D0%60ps%3D1&src_module=search&src_tab_page_id=page_real_class1710429091661")
  list = driver.find_elements(By.CSS_SELECTOR, '.product-list__item-new')

  # 滚动至元素可见，这个经常用
  while list.__len__() != 0:
    # 滑动到页面的特定位置，例如滑动到页面的底部
    driver.execute_script("window.scrollTo(2000, document.body.scrollHeight)")
    # productList = []
    for e in list:
      # 如果需要叉掉 先处理
      # closeElements = driver.find_elements(By.CSS_SELECTOR,'.she-close')
      # for closeElement in closeElements:
      #   ActionChains(driver).move_to_element(closeElement).click()
      driver.execute_script(
          'document.querySelectorAll(\'.she-close\')[0].click()')
      driver.execute_script(
          'document.querySelectorAll(\'.dialog-top__close-btn\')[0]?.click()')

      productLink = e.find_element(By.CSS_SELECTOR, '.goods-title-link--jump')

      productPriceElement = e.find_element(By.CSS_SELECTOR,
                                           '.product-item__camecase-wrap > span')

      productImageElement = e.find_element(By.CSS_SELECTOR,
                                           '.crop-image-container')
      # 产品排名
      productRankElement = e.find_element(By.CSS_SELECTOR,
                                           '.rank-title')
      # 产品链接
      productUrl = productLink.get_attribute('href')
      productName = productLink.get_attribute('aria-label')
      sku = productLink.get_attribute('data-sku')
      spu = productLink.get_attribute('data-spu')
      # 产品名称
      # productName = productLink.text
      if productName == '':
        print('No product')
      productName = productName[:500]
      # 产品价格
      productPrice = productPriceElement.text
      # 产品图片
      productImage = productImageElement.get_attribute('data-before-crop-src')
      # 是否是本土
      nacionalElement = e.find_elements(By.CSS_SELECTOR, '.tag-QuickShip')
      nacionalName = ''
      if len(nacionalElement) > 0:
        nacionalName = nacionalElement[0].text
      # 评论数
      startTextElement = e.find_elements(By.CSS_SELECTOR, '.start-text')
      startText = 0
      if len(startTextElement) > 0:
        startText = startTextElement[0].text
      # 最近售出
      nearSaleCountElement = e.find_elements(By.CSS_SELECTOR,
                                             '.product-card__selling-proposition-text')
      nearSaleCount = 0
      if len(nearSaleCountElement) > 0:
        nearSaleCount = nearSaleCountElement[0].text
      product = Product(productUrl, productName, productPrice, productImage,
                        nacionalName, startText, nearSaleCount, sku, spu)
      # productList.append(product)
      # print(product.__dict__)

      existData = session.query(
          Product,
      ).filter(
          Product.sku == sku
      ).one_or_none()

      if not existData:
        session.add(product)

      # 变更
      else:
        existData.product_url = productUrl
        existData.product_name = productName
        existData.product_price = productPrice
        existData.product_image = productImage
        existData.product_sku = sku
        existData.product_spu = spu
        existData.nacion_name = nacionalName
        existData.start_text = startText

      # 提交
      session.commit()

      # 关闭链接，亦可使用session.remove()，它将回收该链接
      session.close()
    # 发送请求
    url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList'
    headers = {
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
    }
    json = {"portal": 2, "lang": "zh-CN", "country": "CN", "brand": 1,
            "province": "山西", "city": "太原", "pageNo": 1, "pageSize": 20}

    # 直接给post方法中的json参数赋值，赋json类型的数据
    # response = requests.post(url=url, headers=headers, json=json)
    # page_text = response.text

    # print(page_text)
    list = []

    # 判断有没有下一页 有的话继续
    nextButtonElement = driver.find_elements(By.CSS_SELECTOR,
                                             '.sui-pagination__next')
    if len(nextButtonElement) != 0 and (
        nextButtonElement[0].get_attribute('disabled') != 'true'):
      nextButtonElement[0].click()
      WebDriverWait(driver, 100).until(
          EC.presence_of_element_located(
              (By.CSS_SELECTOR, '.goods-title-link--jump'))
      )
      list = driver.find_elements(By.CSS_SELECTOR, '.product-list__item-new')
    else:
      print('全部爬取完毕')


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
  print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
